﻿//-----------------------------------------------------------------------
// <copyright file="ContactFactory.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.Collections.Generic;

    internal sealed class ContactFactory
    {
        private readonly Dictionary<MessengerEntity, Contact> contacts = new Dictionary<MessengerEntity, Contact>(100);

        private readonly User user;

        public ContactFactory(User user)
        {
            this.user = user;
        }

        public Contact FindOrCreate(object obj)
        {
            if (obj == null)
            {
                return null;
            }

            return FindOrCreate(new MessengerEntity(obj));
        }

        public Contact FindOrCreate(MessengerEntity entity)
        {
            Contact contact = null;

            if (this.contacts.TryGetValue(entity, out contact))
            {
                return contact;
            }

            contact = new Contact(entity, this.user);
            this.contacts.Add(entity, contact);
            return contact;
        }
    }
}